﻿/**
作者：常亮
时间：2010-3-25
功能：实现用户增加
**/
var Obj = new Object();
var UploadActionPath = SBasePath + "/action/upload/";
var ContactUsImageArray = new Array();
var ImageArr = new Array('.jpg', '.gif', '.png', '.bmp'); //形象图片类型
var UploadType = 1; //1:缩略图2:原图
var _mDebug = false; //用于调试显示上传显示部分
var ModelID, ModelParentID, leading;
var shenhe;
function ShowMain() {
    ModelID = request("ModelId");
    leading = request("leading");
    ModelParentID = request("ModelParentId");
    ShowContent();
}

var btFormSubmit = function() {
    if (CheckAddForm()) {
        $("bc").disabled = "disabled";
        document.getElementById("shenhe").value = document.getElementById("sh").value;
        var url = encodeURI(SBasePath + '/action/Contents/RelContent.aspx');
        var myAjax = new Ajax.Request(url, { method: 'post', parameters: Form.serialize("formsubmit"), onComplete: sFormSubmit, onFailure: fFormSubmit });
    }
};

var sFormSubmit = function(o) {
    var root = o.responseXML.documentElement;
    var flag = GetXmlData(root, "Value");
    if (flag == "true") {
        MsgDlgLocal('添加成功！');
    }
    else {
        $("bc").disabled = "";
    }
};

var fFormSubmit = function() {
    $("bc").disabled = "";

};

var CheckAddForm = function() {

    var fobj = $("formsubmit");

    if (document.getElementById("titlename").value == "") {
        MsgDlg("标题名称不能为空!");
        fobj.titlename.focus();
        return false;
    }

    return true;
};

function nrsh() {
    shenhe = document.getElementById("sh").value;
}

function ShowContent() {
    var str = "";
    str += '<form id="formsubmit">';
    str += '<input type="hidden" name="ShrinUrl" id="ShrinUrl" value="">';
    str += '<input type="hidden" name="ModelID" id="ModelID" value="' + ModelID + '">'
    str += '<input type="hidden" name="ModelParentID" id="ModelParentID" value="' + ModelParentID + '">';
    str += '<input type="hidden" name="shenhe" id="shenhe" value="' + shenhe + '">';
    str += '<table width="100%" height="100%" border="0" align="center" cellpadding="0" cellspacing="0">';
//    str += '<OBJECT id="dlgHelper" CLASSID="clsid:3050f819-98b5-11cf-bb82-00aa00bdce0b" width="0px" height="0px"></OBJECT>';
    str += ' <tr>';
    str += '   <td height="30"><table width="100%" border="0" cellspacing="0" cellpadding="0">';
    str += '     <tr>';
    str += '       <td width="15" height="30" class="tab03"></td>';
    str += '       <td class="tab05"><div class="title1"  style="padding-top:2px"><img src="../../img/1.gif" height="20" width="19"/>内容发布</div></td>';
    str += '       <td width="14" class="tab07"></td>';
    str += '     </tr>';
    str += '   </table></td>';
    str += ' </tr>';
    str += ' <tr>';
    str += '   <td valign="top"><table width="100%" border="0" cellspacing="0" cellpadding="0">';
    str += '     <tr>';
    str += '       <td width="9" class="tab12"></td>';
    str += '       <td bgcolor="#f3ffe3" valign="top"><table width="100%" border="0" cellspacing="0" cellpadding="0">';
    str += ' <tr>';
    str += '   <td class="k_hui" valign="top"><table width="100%" border="0" cellspacing="2" cellpadding="0" align="center" class="bg_w">';
    str += '  <tr class="bg_g">';
    str += '    <td width="10%" height="30" class="zw" align="center">文件头</td>';
    str += '    <td width="90%" class="zw" style="padding-left:3px">';
    str += '        <input type="text" name="headname" style="width:50%; border:solid 1px #cadcb2;"/><font color="red">【如:中共阳东县委】</font>';
    str += '    </td>';
    str += '  </tr>';

    str += '  <tr class="bg_g">';
    str += '    <td width="10%" height="30" class="zw" align="center">内容审核</td>';
    str += '    <td width="90%" class="zw" style="padding-left:3px">';
    str += '     <select name="sh" id="sh"  onChange="nrsh()"  title="内容审核" size="1" style="width:100px; height:20px " class="k_green">';
    str += '                    <OPTION VALUE="1">通过</OPTION>';
    str += '					<OPTION VALUE="0">未通过</OPTION>';
    str += '      </select>';
    str += '    </td>';
    str += '  </tr>';

    str += '  <tr class="bg_g">';
    str += '    <td width="10%" height="30" class="zw" align="center">发布时间</td>';
    str += '    <td width="90%" class="zw" style="padding-left:3px">';
    str += '    <input type="text" name="fbtime" id="fbtime" style="width:50%; border:solid 1px #cadcb2;"/><font color="red">【时间格式：2010-3-1】</font>';
    str += '    </td>';
    str += '  </tr>';

    str += '  <tr class="bg_g">';
    str += '    <td width="10%" height="30" class="zw" align="center">信息内容</td>';
    str += '    <td width="90%" class="zw" style="padding-left:3px">';
    str += '           <div id="_CommentTextarea">';
    str += '	            <textarea id="_CommentDataTextarea" name="CommentContent"  cols="90" rows="6" style="width: 95%"></textarea>';
    str += '	       </div>';
    str += '	       <div id="_CommentFCKeditor" style="display:none">';
    str += '                 <textarea id="_CommentDataFCKeditor" cols="90" rows="6" style="width: 95%"></textarea>';
    str += '	        </div>';
    str += '    </td>';
    str += '  </tr>';

    str += '</form>';
    
    str += '  <tr class="bg_g">';
    str += '    <td  height="15"  colspan="2" class="zw" align="center">';
    str += '                <input type="button"  value="保存" width="100px" id="bc" onclick="PrepareSave(\'Comment\');btFormSubmit();"   />';
    str += '                <input type="button"  value="返回" id="back" onclick="Back()" class="m_button" />';
    str += '  </td></tr>';

    str += '</table></td>';
    str += '  </tr>';
    str += '</table></td>';
    str += '   <td width="9" class="tab16">';

    str += '      </td></tr>';
    str += '    </table></td>';
    str += '  </tr>';

    str += '  <tr>';
    str += '    <td height="29"><table width="100%" border="0" cellspacing="0" cellpadding="0">';
    str += '      <tr>';
    str += '        <td width="15" height="29" class="tab20"></td>';
    str += '        <td  class="tab21">&nbsp;</td>';
    str += '        <td width="14" class="tab22"></td>';
    str += '      </tr>';
    str += '    </table></td>';
    str += '  </tr>';
    str += '</table>';

    if (_mDebug) {
        str += '  <iframe id="targethidenfram" name="targethidenfram" frameborder="0" height="200" width="400" visible="false"></iframe>';
    }
    else {
        str += '  <iframe id="targethidenfram" name="targethidenfram" frameborder="0" height="0" width="0" visible="false"></iframe>';
    }
    $("Contents").innerHTML = str;
    editor("Comment");

}

//生成副文本编辑器
//参数name是控件Textarea的名称的前面部分主要用于下面组成完整的名称
function editor(name) {
    var oEditor;
    if (typeof (FCKeditorAPI) != 'undefined')
        oEditor = FCKeditorAPI.GetInstance('_' + name + 'DataFCKeditor');
    var eTextareaDiv = document.getElementById('_' + name + 'Textarea');
    var eFCKeditorDiv = document.getElementById('_' + name + 'FCKeditor');

    if (eTextareaDiv.style.display != 'none') {
        if (!oEditor) {
            CreateEditor(name);
        }
        else {
            CreateEditor(name);
        }

        eTextareaDiv.style.display = 'none';
        eFCKeditorDiv.style.display = '';
        if (oEditor && !document.all) {
            if (oEditor.EditMode == FCK_EDITMODE_WYSIWYG)
                oEditor.MakeEditable();
        }
    }
    else {
        document.getElementById('_' + name + 'DataTextarea').value = oEditor.GetXHTML();
        eTextareaDiv.style.display = '';
        eFCKeditorDiv.style.display = 'none';
    }
}

function CreateEditor(name) {
    document.getElementById('_' + name + 'DataFCKeditor').value = document.getElementById('_' + name + 'DataTextarea').value;
    var sBasePath = document.location.pathname.substring(0, document.location.pathname.lastIndexOf('AdminManage'));
    var oFCKeditor = new FCKeditor('_' + name + 'DataFCKeditor');
    oFCKeditor.BasePath = SBasePath + "/";
    oFCKeditor.Width = '700';
    oFCKeditor.Height = '400';
    oFCKeditor.ReplaceTextarea();
}

function FCKeditor_OnComplete(editorInstance) {
}

function PrepareSave(name) //进行保存
{
    if (document.getElementById('_' + name + 'Textarea').style.display == 'none') {
        var oEditor = FCKeditorAPI.GetInstance('_' + name + 'DataFCKeditor');
        document.getElementById('_' + name + 'DataTextarea').value = oEditor.GetXHTML();
    }
}



/*-------------------------------------------以下关于上传-------------------------------------------------*/
var progressWindow;
function StartUpload(formObj, Type)//开始上传
{
    UploadType = Type;
    if (chkGroupImageFileValid()) {
        if (typeof (formObj) == "string")
            formObj = document.getElementById(formObj);
        //Show Cancel button
        var CancelButton = document.getElementById("CancelButton");
        if (CancelButton && PowUploadCanCancel())
            CancelButton.style.visibility = "visible";

        var uniqueID = Math.floor(Math.random() * 10000000) + (new Date()).getTime() % 1000000000;
        progressWindow = ShowProgressInFrame(uniqueID, UploadActionPath + 'Progress.aspx', 400, 200); //显示进度条	

        thePos = formObj.action.indexOf("UploadID");
        //Cut earlier added UploadID.
        if (thePos >= 1)
            formObj.action = formObj.action.substring(0, thePos - 1);
        if (formObj.action.indexOf("?") == -1)
            formObj.action += '?UploadID=' + uniqueID
        else
            formObj.action += '&UploadID=' + uniqueID;
        formObj.submit();
        return true;
    }
}

//Function ShowProgressInNewWindow() to show progress bar in new window.
function ShowProgressInFrame(uniqueID, URL, width, height) {
    var l = (screen.width - width) / 2;
    var t = (screen.height - height) / 2;
    //	var winParams = 'toolbar=no,scrollbars=no,location=no,status=no,menubar=no,resizable=yes';
    //	winParams += ',width=' + width + ',height=' + height + ',left=' + l + ',top=' + t;
    var fullURL = URL;
    thePos = fullURL.indexOf("UploadID");
    //Cut earlier added UploadID.
    if (thePos >= 1)
        fullURL = fullURL.substring(0, thePos - 1);
    if (URL.indexOf("?") == -1)
        fullURL += '?UploadID=' + uniqueID;
    else
        fullURL += '&UploadID=' + uniqueID;
    $("progressout").innerHTML = '<iframe id="progressframe" name="' + uniqueID + '" frameborder="0" height="' + height + '" width="' + width + '" visible="true" src="' + fullURL + '" SCROLLING="no"></iframe>';
    //var newProgressWindow = window.open(fullURL, uniqueID, winParams);
    var newProgressWindow = $("progressframe");
    return newProgressWindow;
}

//上传成功后的回调函数
function UploadCommplete(fobjs) {

    var str = "";
    var filename = "";
    if (fobjs != null) {
        for (var i = 0; i < fobjs.length; i++) {
            str += fobjs[i].FileName + "<br>";
            if (fobjs[i].FileName != "") {
                filename = fobjs[i].FileName;
            }
        }
    }

    $("progressout").style.display = "none";
    if (UploadType == 1) {
        //  $("ShrinImageDiv").innerHTML = '<img src="/web' + filename + '" width="300" height="300" />';
        $("ShrinImageDiv").innerHTML = '<a href="/web' + filename + '" target="_blank" >上传成功</a>';
        $("ShrinUrl").value = filename;
    }
    else {
        ContactUsImageArray[ContactUsImageArray.length++] = filename;
        ShowContactUsImage(ContactUsImageArray);
    }


}

//检查形象图片的合法性
function chkGroupImageFileValid() {
    var objs = null;
    if (UploadType == 1) {

        objs = $("file1");
    }
    else {
        objs = $("file2");
    }

    if (objs.value == "") {
        MsgDlg("您还没有选择任何文件!");
        return false;
    }
    else {
        var name = objs.value;
        name = name.substring(name.lastIndexOf('.'));
        for (var i = 0; i < ImageArr.length; i++) {
            if (name.toLowerCase() == ImageArr[i]) {
                return true;
            }
        }
        MsgDlg("您所选择的文件类型不对!");
        return false;
    }
}